#include <fstream>
#include <iostream>
#include "piece.hpp"

Piece::Piece(int pieceId)
{
	id = pieceId;
	r = 0;
	x = 3;
	y = 0;

	std::string filenames[7] = {
		"piece/1.dat",
		"piece/2.dat",
		"piece/3.dat",
		"piece/4.dat",
		"piece/5.dat",
		"piece/6.dat",
		"piece/7.dat"
	};
	for(int i = 0;i < 7;i++)
	{
		std::ifstream file(filenames[i].c_str());
		for(int _r = 0;_r < 4;_r++)
			for(int _y = 0;_y < 4;_y++)
				for(int _x = 0;_x < 4;_x++)
				{
					file >> pieces[i][_r][_y][_x];
					pieces[i][_r][_y][_x] -= '0';
				}
		file.close();
	}
}

char Piece::get(int _y, int _x)
{
	return pieces[id][r][_y][_x];
}

void Piece::rotate(bool clockwise)
{
	if(clockwise)
		r++;
	else
		r--;

	if(r>3)
		r=0;
	else if(r<0)
		r=3;
}
